<?php
require_once("dao/IDao.php");
require_once("model/Venue.php");
require_once("model/Photo.php");

class VenueDAO implements IDao{
	var $conn;
	var $column = array("photo_id","image_type","image_name","ext","image_data_thumb");
	var $imgFile;	
	
	public function __construct($conn){
		$this->conn =& $conn;
	}
	
	public function getItem($venueid){
		$venue = null;
		try{
			if($stmt = $this->conn->prepare("CALL VENUE_READ_ITEM(?)")){
				$stmt->bind_param("i",$venueid);
				$stmt->execute();
				$stmt->bind_result($_venueid,$_venuename,$_streetaddress1,$_streetaddress2,$_city,$_statecode,$_zipcode,$_phone,$_webaddress,$_emailaddress,$_profphotoid,$_hasphoto);
				
				while($stmt->fetch()){
					$venue = new Venue($_venueid, $_venuename);
					$venue->streetaddress1 = $_streetaddress1;
					$venue->streetaddress2 = $_streetaddress2;
					$venue->city = $_city;
					$venue->statecode = $_statecode;
					$venue->zipcode = $_zipcode;
					$venue->phone = $_phone;
					$venue->webaddress = $_webaddress;
					$venue->emailaddress = $_emailaddress;
					$venue->hasphoto = ($_hasphoto == 1);
					$photo = new Photo();
					$photo->photoId = $_profphotoid;
					$venue->photo = $photo;
				}
				if($stmt->error != ""){
					throw  new Exception("Statement preparation failed in class:" . __CLASS__ . "\nError: ". $stmt->error);
				}
			}
			else{
				throw  new Exception("Statement preparation failed in class:" . __CLASS__);
			}
		}
		catch(Exception $e){
			throw new Exception("Statement preparation failed in class:" . __CLASS__."\t".$e->getMessage());
		}
		
		$stmt->close();
		return $venue;
	}
	
	public function getPhoto($venueid){
		$data = array();
		$imgFile = null;
		try{
			if($stmt = $this->conn->prepare("CALL VENUE_GET_PHOTO(?)")){
				$stmt->bind_param("i",$venueid);
				$stmt->execute();
				$stmt->store_result();
				$params = array();
				foreach($this->column as $col_name){
					$params[] =& $data[$col_name];
				}
				$res = call_user_func_array(array($stmt,"bind_result"),$params);
				if(!$res){
					echo "bind_result() failed: ". $this->conn->error ."\n";
				}
				else{
					$res = $stmt->fetch();
					if($res){}else{
						echo $stmt->error;
					}
				}
			}
			else{
				throw  new Exception("Statement preparation failed in class:" . __CLASS__);
			}
		}
		catch(Exception $e){
			throw new Exception("Statement preparation failed in class:" . __CLASS__."\t".$e->getMessage());
		}
		
		$stmt->close();
		if(isset($data["photo"])){
			$imgFile = new ImageFile();
			$imgFile->data = $data["photo"];
			$imgFile->type = $data["imgtype"];
		}
		return $imgFile;
	}
	
	public function saveItem($venue){
		try{
			if($stmt = $this->conn->prepare("CALL VENUE_UPDATE_ITEM(?,?,?,?,?,?,?,?,?,?)")){
				$stmt->bind_param("isssssssss",$venue->venueid,$venue->venuename,$venue->address1,$venue->address2,$venue->city,$venue->statecode,$venue->zipcode,$venue->phone,$venue->webaddress,$venue->emailaddress);
				$stmt->execute();
				if($stmt->error != "")
					throw new Exception("Error occurred in saveItem method in ". __CLASS__ . "\n" . $stmt->error);
			}
			else{
				throw new Exception("Error occurred in saveItem method in ". __CLASS__ . "\n" . $stmt->error);
			}
		}
		catch(Exception $e){
			printf("Error occurred in preparing the statement for PlayerMgmt::getAppearance<br/>Details:%s", $stmt->error);
		}
	}
	
	public function deleteItem($venueid){
		
	}
	
	public function readList($venueid,$venueName,$city){
		$result = null;
		try{
			if($stmt = $this->conn->prepare("CALL VENUE_READ_LIST(?,?,?)")){
				$stmt->bind_param("iss",$venueid,$venueName,$city);
				$stmt->execute();
				$stmt->bind_result($_venueid,$_venuename,$_city,$_statecode,$_zipcode);
				$result = array();
				while($stmt->fetch()){
					$venue = new Venue($_venueid, $_venuename);
					$venue->city = $_city;
					$venue->statecode = $_statecode;
					$venue->zipcode = $_zipcode;
					array_push($result,$venue);
				}
			}
			else{
				printf("Error occurred in preparing the statement for readlist in %s",__CLASS__);
			}
			
			if($stmt->error != "")
				printf("Failed to prepare statement.<br/>%s",$stmt->error);
		}
		catch(Exception $e){
			printf("Error occurred in preparing the statement for readList in %s<br/>Details:%s", __CLASS__,$e->getMessage());
		}
		
		return $result;
	}
	
	public function addPhoto($venueid){
		
	}
	
	public function updatePhoto($venueid,$image){
		try{
			if($stmt = $this->conn->prepare("CALL VENUE_UPDATE_PHOTO(?,?,?,?,?,?,?,?,?)")){
				$null = null;
				$stmt->bind_param("isbbbsiss",$venueid,$image->caption,$null,$null,$null,$image->imageType,$image->imageSize,$image->imageName,$image->ext);
				$stmt->send_long_data(2,$image->imageDataLarge);
				$stmt->send_long_data(3,$image->imageDataMed);
				$stmt->send_long_data(4,$image->imageDataThumb);
				$stmt->execute();
					
				if($stmt->error != "")
					echo printf("Statement prepation failed in %s<br/>%s<br/>",__CLASS__, $stmt->error);
			
				$stmt->close();
			}
			else{
				throw new Exception("Statement preparation failed at ".__CLASS__.".");
			}	
		}
		catch(Exception $e){
			echo "Error occurred in preparing the statement for ".__CLASS__." in updatePhoto method.<br/>Details:".$e->getMessage();
		}
	}
	
	public function readPhotoList($venueid){
		try{
			if($stmt = $this->conn->prepare("CALL VENUE_PHOTO_READ_LIST(?)")){
				$stmt->bind_param("i",$venueid);
				$stmt->execute();
				$stmt->store_result();
				$params = array();
				foreach($this->column as $col_name){
					$params[] =& $data[$col_name];
				}
				$res = call_user_func_array(array($stmt,"bind_result"),$params);
				if(!$res){
					echo "bind_result() failed: ". $this->conn->error ."\n";
				}
				else{
					$res = $stmt->fetch();
					if($res){}else{
						echo $stmt->error;
					}
				}
				
				if($stmt->error != "")
					echo printf("Statement prepation failed in %s<br/>%s<br/>",__CLASS__, $stmt->error);
				$stmt->close();
			}
			else{
				throw new Exception("Statement preparation failed at ".__CLASS__.".");
			}
		}
		catch(Exception $e){
			echo "Error occurred in preparing the statement for ".__CLASS__." in \"readPhotoList\" method.<br/>Details:".$e->getMessage();
		}
	}
	
	public function __destruct(){$this->conn->close();}
}

?>